package com.gykj.tap.config;

import java.util.concurrent.Executor;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

/**
 * 多线程任务，可以确保每一个任务都是一个线程
 * @author rohto
 * 用法：在定时任务的类或者方法上添加@Async
 */
@Configuration
@EnableAsync //开启异步事件的支持
public class AsyncConfig {

	//
	@Value("${spring.schedule.corePoolSize}")
	private String corePoolSize;
	
	@Value("${spring.schedule.maxPoolSize}")
	private String maxPoolSize;
	
	@Value("${spring.schedule.queueCapacity}")
	private String queueCapacity;

	@Bean
	public Executor taskExecutor() {
		ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
		executor.setCorePoolSize(Integer.parseInt(corePoolSize));
		executor.setMaxPoolSize(Integer.parseInt(maxPoolSize));
		executor.setQueueCapacity(Integer.parseInt(queueCapacity));
		executor.initialize();
		return executor;
	}
}
